package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.dao.PlaceUnitHandelTypeDao;
import com.quanyan.ic.datacenter.dao.PlaceUnitVideoAlarmHandleDao;
import com.quanyan.ic.datacenter.http.req.ReqHandleAlarm;
import com.quanyan.ic.datacenter.mapper.icData.PlaceUnitVideoAlarmHandleMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitHandelType;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitVideoAlarmHandle;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitVideoAlarmHandleExample;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2017/8/25.
 */
@Repository
public class PlaceUnitVideoAlarmHandleDaoImpl implements PlaceUnitVideoAlarmHandleDao {

    @Autowired
    private PlaceUnitVideoAlarmHandleMapper placeUnitVideoAlarmHandleMapper;

    @Autowired
    private PlaceUnitHandelTypeDao placeUnitHandelTypeDao;

    @Override
    public PlaceUnitVideoAlarmHandle findByUuid(Integer uuid) {
        PlaceUnitVideoAlarmHandleExample example = new PlaceUnitVideoAlarmHandleExample();
        example.createCriteria().andAlarmUuidEqualTo(uuid);
        List<PlaceUnitVideoAlarmHandle> handleList = placeUnitVideoAlarmHandleMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(handleList)) {
            return null;
        }
        return handleList.get(0);
    }

    @Override
    public void insertPlaceUnitVideoAlarmHandle(PlaceUnitVideoAlarmHandle handle) {
        placeUnitVideoAlarmHandleMapper.insertSelective(handle);
    }

    @Override
    public void updatePlaceUnitVideoAlarmHandle(PlaceUnitVideoAlarmHandle handle) {
        placeUnitVideoAlarmHandleMapper.updateByPrimaryKeySelective(handle);
    }

    public void  handel(ReqHandleAlarm reqHandleAlarm){
       PlaceUnitVideoAlarmHandle update = new PlaceUnitVideoAlarmHandle();
       update.setId(reqHandleAlarm.getId());
       update.setHandleTypeId(reqHandleAlarm.getType());
       update.setHandleRemark(reqHandleAlarm.getRemark());

       PlaceUnitHandelType type =  placeUnitHandelTypeDao.findPlaceUnitHandelType(reqHandleAlarm.getType());
       if (type != null)
          update.setHandleType(type.getTypeName());
       placeUnitVideoAlarmHandleMapper.updateByPrimaryKeySelective(update);
    }

    public int isHaveAlarm(Integer placeId){
        PlaceUnitVideoAlarmHandleExample example = new PlaceUnitVideoAlarmHandleExample();
        PlaceUnitVideoAlarmHandleExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        criteria.andHandleTypeIdEqualTo(5);
        return placeUnitVideoAlarmHandleMapper.countByExample(example);
    }
}
